home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / AEA / Source / Sources / Event Handlers / AEAHandler.cc next >
Encoding:
Text File  |  2000-06-24  |  3.0 KB  |  133 lines

  1. /*    =============
  2.  *    AEAHandler.cc
  3.  *    =============
  4.  */
  5.  
  6. //#define Debug_Throw
  7. //#define Debug_Signal
  8. #include "AEADebugging.h"
  9.  
  10. // AEA
  11. #include "AEADescAppleEvent.hh"
  12. #include "AEAHandler.hh"
  13.  
  14. AEEventHandlerUPP
  15. AEAHandler::sAEEventHandlerUPP = NewAEEventHandlerProc(GenericAEHandler);
  16.  
  17. AEAHandler::AEAHandler(AEEventClass inEventClass, AEEventID inEventID)
  18. : mEventClass(inEventClass), mEventID(inEventID)
  19. {
  20.     Install();
  21. }
  22.  
  23. AEAHandler::~AEAHandler()
  24. {
  25.     Remove();
  26. }
  27.  
  28. void
  29. AEAHandler::Install()
  30. {
  31.     OSErr err;
  32.     
  33.     err = ::AEInstallEventHandler(mEventClass, mEventID, sAEEventHandlerUPP, (long)this, false);
  34.     
  35.     ThrowIfOSErr_(err);
  36. }
  37.  
  38. void
  39. AEAHandler::Remove()
  40. {
  41.     OSErr err;
  42.     
  43.     err = ::AERemoveEventHandler(mEventClass, mEventID, sAEEventHandlerUPP, false);
  44.     
  45.     ThrowIfOSErr_(err);
  46. }
  47.  
  48. void
  49. AEAHandler::MakeNoteToTouchParam(AEKeyword inParamKey)
  50. {
  51.     mParamsToTouch.Append(inParamKey);
  52. }
  53.  
  54. void
  55. AEAHandler::TouchParameters(const AEADescAppleEvent &inAppleEvent)
  56. {
  57.     OSErr err;
  58.     DescType actualType;
  59.     Size actualSize;
  60.     AEKeyword key;
  61.     
  62.     key = mParamsToTouch.FirstDatum();
  63.     while (key != (AEKeyword)NULL) {
  64.         inAppleEvent.GetParameter(key, typeWildCard, 
  65.             actualType, NULL, (Size)0, actualSize);
  66.         key = mParamsToTouch.Successor(key); // not reliable for 2x keys
  67.     }
  68. }
  69.  
  70. void
  71. AEAHandler::HandleEvent(const AEADescAppleEvent &inAppleEvent, AEADescAppleEvent &outReply)
  72. {
  73.     OSErr err;
  74.     DescType actualType;
  75.     Size actualSize;
  76.     
  77.     TouchParameters(inAppleEvent);
  78.     
  79.     try {
  80.         //SetDebugThrow_(debugAction_Nothing); // this isn't nice
  81.         inAppleEvent.GetAttribute(keyMissedKeywordAttr, 
  82.             typeWildCard, actualType, NULL, (Size)0, actualSize);
  83.         //SetDebugThrow_(debugAction_Alert);
  84.         // If we've gotten all the parameters, then the keyword will be missing,
  85.         // and GetAttribute will throw errAEDescNotFound.
  86.         // We only get to here if the keyword is present and we missed parameters.
  87.         throw errAEParamMissed;
  88.     } catch (ExceptionCode code) {
  89.         //SetDebugThrow_(debugAction_Alert);
  90.         if (code != errAEDescNotFound)
  91.             throw code;
  92.     }
  93.     HandleEventSelf(inAppleEvent, outReply);
  94. }
  95.  
  96. void
  97. AEAHandler::HandleEventSelf(const AEADescAppleEvent &, AEADescAppleEvent &)
  98. {
  99.     ThrowOSErr_(errAEEventNotHandled);
  100. }
  101.  
  102. /*
  103.  *    GenericAEHandler
  104.  *    
  105.  *    This function gets installed as an Apple event handler with AEInstallEventHandler
  106.  *    for each AEAHandler object that gets constructed.
  107.  *    
  108.  *    inRefCon is a pointer to the AEAHandler.
  109.  */
  110.  
  111. pascal OSErr
  112. AEAHandler::GenericAEHandler(const AppleEvent *inAppleEvent, AppleEvent *outReply, long int inRefCon)
  113. {
  114.     // If we don't have the real handler, we can't do much.
  115.     // We should probably signal an error.
  116.     if (inRefCon == NULL) return errAEEventNotHandled;
  117.     
  118.     // Throwing out of a callback is probably bad. Don't do it.
  119.     try {
  120.         const AEADescAppleEvent appleEvent(*inAppleEvent);
  121.         AEADescAppleEvent reply(*outReply);
  122.         
  123.         ((AEAHandler *)inRefCon)->HandleEvent(appleEvent, reply);
  124.     } catch (ExceptionCode code) {
  125.         return code;
  126.     } catch (...) {
  127.         //SignalPStr_("\pThis sucks!");
  128.         return errAEEventNotHandled;
  129.     }
  130.     
  131.     return noErr;
  132. }
  133.